#!/bin/sh

REMEMBERRC="$HOME/.rememberrc"
REMEMBERRC="/home/pamiec/qpq/aaa_skrypty/remember/.rememberrc"

[ -s ${REMEMBERRC} ] || { echo "no rememberrc"; exit 0; }
case "$1" in
  sms)
    COMMAND=`grep '^COMMAND' $REMEMBERRC`
    [ -z "$COMMAND" ] && { echo 'COMMAND not set'; exit 1; }
    COMMAND=`expr "$COMMAND" : 'COMMAND\ *=\ *\(.*\)'`
    [ -z "$COMMAND" ] && { echo 'COMMAND not set'; exit 1; }
    ;;
  login)
    COMMAND=cat
    ;;
  *)
    echo "What? $0 [sms|login] [rok|tydzien]"
    exit 1
esac
case "$2" in
	rok)
		get_event()
		{
			TMP="`date -d "$1" +'^%d-%m|^%d-\*'`"
			TMP="`egrep "$TMP" $REMEMBERRC | cut -d " " -f 2-`"
			[ -n "$TMP" ] && echo "${2} ${TMP}#"
		}
		TEXT=""
		# TEXT="$TEXT`get_event '-1 day' 'Wczo '`"
		TEXT="$TEXT`get_event ' 0 day' 'Dzis '`"
		TEXT="$TEXT`get_event ' 1 day' 'Jutro'`"
		for i in 2 3 4 5 6
		do
			TEXT="$TEXT`get_event "$i day" "Za ${i}d"`"
		done
		TEXT="$TEXT$(get_event '1 week' 'In 1w:')"
		[ -n "$TEXT" ] && { echo -n "$TEXT" | sed -e 'y/#/\n/' | $COMMAND; }
		;;
	tydzien)
		{
		cat $REMEMBERRC | egrep "^p [0-9][0-9]:[0-9][0-9] [mon|tue|wed|thu|fri|sat|sun]" | sed 's/^p //' | \
		while read line; do
			a1=$(echo $line | awk {'print $1'} ) # hour like 01:20
			a2=$(echo $line | awk {'print $2'} ) # dzien tygodnia like 
			a3=$(echo $line | cut -d ' ' -f 3- ) # co ma byc wyswietlone
			sec_now=$(date +%s)
			sec_then=$(date -d "$a1 $a2" +%s)
			sec_00_now=$(date -d "00:00" +%s)
			sec_00_then=$(date -d "00:00 $a2" +%s)
			days=$[ ($sec_00_then-$sec_00_now)/(60*60*24) ]
			TEXT=""
			if test "$sec_then" -gt "$sec_now" -a "$sec_then" -lt "$(date -d '+3Days 00:00' +%s)"; then
				case $days in
					0)  text="Dzis " ;;
					1)  text="Jutro" ;;
					*)  text="Za ${days}d" ;;
				esac
				echo "$text ${a3}"
			fi
		done
		} | $COMMAND
		;;
	oba)
		{ $0 login rok; $0 login tydzien; } | sort | $COMMAND
		;;
	*)
		echo "What? $0 [sms|login] [rok|tydzien|oba]"
		exit 1
esac


